

clear all
set more off
capture log close
set mat 5000


cd "C:\Users\rujia\Dropbox\coalmine\restat"

use  Coalmine.dta, clear

drop if native==.
drop if leader_id==.

gen ln_output = log(prod0)

gen prod0_normalized=prod0/10000
label var prod0_normalized "Output"


label var ln_output "ln Output"


bysort prov_id: egen pre_deathrate=mean(deathrate) if year<1998
bysort prov_id: egen m_pre_deathrate=mean(pre_deathrate) 
gen pre_year=m_pre_deathrate*year
gen decentralized=(year>=1998 & year<=2000)

tabu prov_id, gen (pdummy)
forvalues i=1(1)22{
gen prov_year`i'=pdummy`i'*year
}

gen period=1 if year<1998
replace period=2 if year>=1998 & year<=2000
replace period=3 if year>2000 & year<=2005

egen clusterg=group(prov_id period)

gen decentralized_native=decentralized*native

gen ln_wage=log(indwage)
gen tenure=year-startyear+1
gen ln_gdpper=log(gdpper)
gen ln_electricity=log(eleconsum)
gen ln_disbeijing=log(1+dis_beijing)


local controls "ln_output ln_wage tenure age ln_gdpper ln_electricity gov_native s_native ln_disbeijing"


foreach var in ln_output ln_wage tenure age ln_gdpper ln_electricity gov_native s_native ln_disbeijing{
gen decentralized_`var'=decentralized*`var'
}



merge 1:1 prov_id year using LocalCoalmine.dta



preserve

keep prov_id year deathrate ln_output

gen state=1

save data/keystate.dta, replace

restore

preserve

drop  deathrate ln_output _m

save data/Prov_Leader.dta, replace

restore

use data/LocalCoalmine.dta, replace
gen state=0
gen deathrate=deathrate_l
gen ln_output=log(coal_l)
append using data/keystate.dta

drop if deathrate==.

merge m:1 prov_id year using data/Prov_Leader.dta

drop if year<1995
drop if year>2005
drop _m



sum decentralized

gen decentralized_state = decentralized*state

gen native_state=native*state

gen decentralized_native_state=decentralized_native*state


sum decentralized_native_state decentralized_native decentralized_state native_state  decentralized state native


label var decentralized_native_state "Decentralization * Native * Key State"
label var decentralized_native "Decentralization * Native"
label var decentralized_state  "Decentralization * Key State"
label var native_state  "Native * Key State"
label var state "Key State"
label var native "Native"


#delimit ;

eststo clear;

eststo: xi:areg deathrate decentralized_native_state decentralized_native decentralized_state native_state  state native 
i.prov_id i.year, absorb(prov_id)cluster(clusterg);
estadd local pfe "Y";
estadd local yfe "Y";


estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);

eststo: xi:areg deathrate decentralized_native_state decentralized_native decentralized_state native_state  decentralized state 
i.prov_id i.year, absorb(leader_id)cluster(clusterg);
estadd local pfe "Y";
estadd local yfe "Y";
estadd local rfe "Y";

estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);

eststo: xi:areg deathrate decentralized_native_state decentralized_native decentralized_state native_state  decentralized state
i.prov_id i.year `controls', absorb(leader_id)cluster(clusterg);
estadd local pfe "Y";
estadd local yfe "Y";
estadd local rfe "Y";
estadd local controls "Y";
estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);

eststo: xi:areg deathrate decentralized_native_state decentralized_native decentralized_state native_state  decentralized state 
i.prov_id i.year `controls' decentralized_*, absorb(leader_id)cluster(clusterg);
estadd local pfe "Y";
estadd local yfe "Y";
estadd local rfe "Y";
estadd local controls "Y";
estadd local controls_d "Y";

estadd scalar nobs=e(N);
estadd scalar rsq=e(r2);


esttab using draft/DDD.tex, replace
	width(\hsize) 
	nomtitles
	b(3)
	se(3)
	noconstant
	star(* 0.10 ** 0.05 *** 0.01)
	keep(
decentralized_native_state 
decentralized_native 
decentralized_state 
native_state 
state
native
		)
	label
	stats(
		pfe 
		yfe
		rfe
		controls
		controls_d
	 
		nobs
		rsq
		, 
		labels(
			"Province FE" 
			"Year FE"
			"Regulator FE"
			"Controls"
			"Decentralization * Controls"
		   
			"\# observations"
			"R-Squared"
			) 
		fmt(0 0 0 0 0 0  2)
		layout(@)
		)
	nonotes
	addnotes(
	)
	;
